// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
/**
 * 签到
 */
let base = require("panelBase")
let viewConfig = {
    btn_back:{
        node:null,
        name:"btn_back",
        eventType:"click",
        cb:"onBtnBackClick"
    },
    //签到奖励
    sign_des:{
        node:null,
        name:"sign_re_des"
    },
    //签到天数
    sign_total_date:{
        node:null,
        name:"white_bg/sign_total_date",
    },
    banner:{
        node:null,
        name:"banner_bg",
        active:false,
    }
};
cc.Class({
    extends: base,

    properties: {
        signList:{
            type:cc.Node,
            default:[],
        },
        maxSignDate:0,
    },

    init(){
        this._super(viewConfig);
        let parent = viewConfig["sign_total_date"].node.parent;
        let count = 0;
        for(let i = 0 ; i < parent.childrenCount;++i)
        {
            let child = parent.children[i];
            if(child.name.indexOf("btn") > -1)
            {
               
                count++;
                child.date = count;
                this.signList.push(child);
            }
        }

        cc.tg.eventCenter.addEvent("updateSign",()=>{
            this.updateSign();
        });
        this.maxSignDate = 7;
        this.isInited = true;
        this.onHide();
    },

    onShow(){
        this._super();
        this.updateSign();
        
        cc.tg.adDataCenter.toShowAd("sign_ex_1");
    },

    updateSign(){
        let signData = cc.tg.cakeRoom.accContinuousSign;
        for(let i = 0; i < this.signList.length;++i)
        {
            this.initSign(this.signList[i],signData);
        }
        this.updateInfo(viewConfig["sign_total_date"].node,signData+"天",cc.Label);
    },


    onBtnBackClick(){
        this.onHide();
    },

    onClickSign(e){
        // cc.log(e.target.date);
        // let date = e.target.date;
        // let offset = date - cc.tg.cakeRoom.accContinuousSign
        // if(offset == 1)
        // {
        //     cc.tg.gameLogic.toSign();

        // }
        // else
        // {
        //     cc.log("不能签到");
        // }

    },

    onHide(){
        this._super();
        cc.tg.adDataCenter.toShowAd("sign_ex_1",true);
    },

    initSign(node,data){
        if(!node || data == null  )
            return ;

        data = data <= this.maxSignDate ? data : this.maxSignDate ;
        let date = node.date;
        let unsign = node.children[0];
        let sign = node.children[1];
        let reward = node.children[2];
        if(date <= data)
        {
            //已经签到
            unsign.active = false;
            sign.active = true;
            reward.active = false;
           // node.getComponent(cc.Button).interactable = false;

            this.updateInfo(sign.getChildByName("date"),"第"+date + "天",cc.Label);
        }
        else
        {
            let isLastOne =  date == this.maxSignDate ? true : false
           // node.getComponent(cc.Button).interactable = true;
            if(isLastOne)
            {
                unsign.active = false;
                reward.active = true;
            }
            else
            {
                unsign.active = true;
                reward.active = false;
                this.updateInfo(unsign.getChildByName("date"),"第"+date + "天",cc.Label);
            }

            sign.active = false;
        }
    },
    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    },
});
